package Services

import (
	"context"
	"github.com/go-kit/kit/endpoint"
	"strings"
)

type UserRequest struct { //封装User请求结构体
	Id     int    `json:"uid"`
	Method string `json:"method"`
}

type UserResponse struct {
	Result any `json:"result"`
}

func GenUserEndPoint(userService IUserService) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (response interface{}, err error) {
		r := request.(UserRequest) //通过类型断言获取请求结构体
		var result any
		if strings.EqualFold(r.Method, "POST") {
			result = userService.Add(User{Id: 1, Name: "lisi"})
		} else if strings.EqualFold(r.Method, "GET") {
			result = userService.Detail(1)
		} else if strings.EqualFold(r.Method, "PUT") {
			result = userService.Edit(User{Id: 1, Name: "zhangsan"})
		} else {
			result = userService.Delete(r.Id) //
		}
		return UserResponse{Result: result}, nil
	}
}
